package ym.ecrm.web.action.systemManagement;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.springframework.beans.factory.annotation.Autowired;

import ym.ecrm.core.common.util.Pager;
import ym.ecrm.core.dto.systemManagement.UserDTO;
import ym.ecrm.core.dto.systemManagement.UserQueryConditionDTO;
import ym.ecrm.core.service.systemManagement.ISystemService;
import ym.ecrm.web.action.base.BaseAction;

/**
 * 
 * @author Princess
 * 
 */
public class ListUserAction extends BaseAction {

	private String userCode;
	private String userName;
	private String state;
	private int page;
	private int limit;
	private Pager<UserDTO> pager;

	@Autowired
	private ISystemService systemService;

	public String execute() {
		if((userCode == null || userCode.equals("")) &&
				(userName == null || userName.equals("")) &&
				(state == null || state.equals(""))){
			pager = systemService.findUsers(page, limit);
			return proccess();
		}
		if (userCode != null && !userCode.equals("")) {
			UserQueryConditionDTO queryConditionDTO = 
					new UserQueryConditionDTO();
			queryConditionDTO.setUserCode(userCode);
			pager = systemService.findUsers(queryConditionDTO, page, limit);
			return proccess();
		}
		if (userName != null && !userName.equals("")) {
			UserQueryConditionDTO queryConditionDTO = 
					new UserQueryConditionDTO();
			System.out.println(userName);
			try {
				queryConditionDTO.setUserName(URLDecoder.decode(userName,"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			pager = systemService.findUsers(queryConditionDTO, page, limit);
			return proccess();
		}
		if (state != null && !state.equals("")) {
			UserQueryConditionDTO queryConditionDTO = 
					new UserQueryConditionDTO();
			try {
				queryConditionDTO.setState(URLDecoder.decode(state,"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			pager = systemService.findUsers(queryConditionDTO, page, limit);
			return proccess();
		}
		pager = systemService.findUsers(page, limit);
		return proccess();

	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public Pager<UserDTO> getPager() {
		return pager;
	}

	public void setPager(Pager<UserDTO> pager) {
		this.pager = pager;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public String getSuccess() {
		return success;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

}
